/**

          _____                    _____                    _____                _____
         /\    \                  /\    \                  /\    \              /\    \
        /::\    \                /::\    \                /::\    \            /::\    \
       /::::\    \               \:::\    \              /::::\    \           \:::\    \
      /::::::\    \               \:::\    \            /::::::\    \           \:::\    \
     /:::/\:::\    \               \:::\    \          /:::/\:::\    \           \:::\    \
    /:::/  \:::\    \               \:::\    \        /:::/__\:::\    \           \:::\    \
   /:::/    \:::\    \              /::::\    \      /::::\   \:::\    \          /::::\    \
  /:::/    / \:::\    \    ____    /::::::\    \    /::::::\   \:::\    \        /::::::\    \
 /:::/    /   \:::\ ___\  /\   \  /:::/\:::\    \  /:::/\:::\   \:::\____\      /:::/\:::\    \
/:::/____/  ___\:::|    |/::\   \/:::/  \:::\____\/:::/  \:::\   \:::|    |    /:::/  \:::\____\
\:::\    \ /\  /:::|____|\:::\  /:::/    \::/    /\::/   |::::\  /:::|____|   /:::/    \::/    /
 \:::\    /::\ \::/    /  \:::\/:::/    / \/____/  \/____|:::::\/:::/    /   /:::/    / \/____/
  \:::\   \:::\ \/____/    \::::::/    /                 |:::::::::/    /   /:::/    /
   \:::\   \:::\____\       \::::/____/                  |::|\::::/    /   /:::/    /
    \:::\  /:::/    /        \:::\    \                  |::| \::/____/    \::/    /
     \:::\/:::/    /          \:::\    \                 |::|  ~|           \/____/
      \::::::/    /            \:::\    \                |::|   |
       \::::/    /              \:::\____\               \::|   |
        \::/____/                \::/    /                \:|   |
                                  \/____/                  \|___|
//桂林信息科技学院GIRT机器人队
  ****************************(C) COPYRIGHT 2023 GIRT****************************
  * @file       can_receive.c/h
  * @brief      there is CAN interrupt function  to receive motor data,
  *             and CAN send function to send motor current to control motor.
  *             这里是CAN中断接收函数，接收电机数据,CAN发送函数发送电机电流控制电机.
  * @note       
  * @history
  *  Version    Date            Author          Modification
  *  V1.0.0     Dec-26-2018     RM              1. done
  *  V1.1.0     Nov-11-2019     RM              1. support hal lib
  *  V1.2.0     March-17-2023   HZY             1. Fit the 23 season suction cup engineering car
  *
  @verbatim
  ==============================================================================

  ==============================================================================
  @endverbatim
  ****************************(C) COPYRIGHT 2023 GIRT****************************
  */

#ifndef CAN_RECEIVE_H
#define CAN_RECEIVE_H

#include "struct_typedef.h"

#define CHASSIS_CAN hcan       //CAN1
//#define GIMBAL_CAN hcan      //CAN2

//limit
#define P_MIN -3.14  	//位置最小值
#define P_MAX 3.14 		//位置最大值
#define V_MIN -30	  	//速度最小值
#define V_MAX 30	  	//速度最大值
#define KP_MIN 0.0		//Kp最小值
#define KP_MAX 500.0	//Kp最大值
#define KD_MIN 0.0		//Kd最小值
#define KD_MAX 5.0		//Kd最大值
#define T_MIN -18		  //转矩最小值
#define T_MAX 18		  //转矩最大值

#define SUCKER_YAW_MOTOR_MATER_ID		  0x011
#define SUCKER_PITCH_F_MOTOR_MATER_ID	0x012
#define SUCKER_PITCH_B_MOTOR_MATER_ID	0x013

#define MIT_MODE		    0x000
#define SPEED_POS_MODE	0x100
#define SPEED_MODE		  0x200

#define SLAVE_Info 0x401 //CAN2发送从控消息帧ID

/* CAN1 send and receive ID */
typedef enum
{
	// CAN_J4310Moto_ALL_ID 	= 0x3FE,    	//达妙4310电机一拖四(ID:1~4)
	
	CAN_SUCKER_YAW_MOTOR_ID = 0x001 + SPEED_POS_MODE,  //吸盘yaw轴电机ID
	CAN_SUCKER_PITCH_F_ID 	= 0x002 + SPEED_POS_MODE,  //吸盘pitch轴前端电机ID
	CAN_SUCKER_PITCH_B_ID 	= 0x003 + SPEED_POS_MODE,  //吸盘pitch轴末端电机ID
} can1_msg_id_e;

/* CAN2 send and receive ID */
typedef enum
{
	CAN_ORE_EXTRACTION_ALL_ID = 0x200, 		//取矿电机数据帧ID
	CAN_RESCUE_ALL_ID		      = 0x1FF,		//救援勾数据帧ID
													  
	CAN_SIDESWAY_MOTOR_ID 	  = 0x201,		//横移电机ID
	CAN_SUCKER_ROLL_MOTOR_ID  = 0x202,		//吸盘roll轴电机ID
	CAN_TELESCOPIC_EX_ML_ID   = 0x203,   	//左外伸缩电机ID
	CAN_TELESCOPIC_EX_MR_ID   = 0x204,   	//右外伸缩电机ID	

	CAN_RESCUE_ML_ID 		      = 0x205,		//左救援勾电机ID
	CAN_RESCUE_MR_ID 		      = 0x206,		//右救援勾电机ID

  CAN_INFO_MASTER_ID        = 0x402,    //传输板发过来主控数据包的帧ID
} can2_msg_id_e;

//rm motor data
typedef struct
{
	uint16_t ecd;                   //转子机械角度
	int16_t speed_rpm;              //转子转速
	int16_t given_current;          //转子转矩电流
	uint8_t temperate;              //电机温度
	int16_t last_ecd;               //上一帧的转子机械角度
	
	int16_t angle;
	int16_t last_angle;
	int32_t total_angle;
	int32_t round_cnt;
} motor_measure_t;

//dm motor data
typedef struct
{
	uint8_t id;
	uint8_t err;
	int p_int;
	int v_int;
	int t_int;
	float position;//int position;
	float velocity;
	float torque;
	float T_MOS;
	float T_Rotor;
	uint16_t last_ecd;
	uint16_t ecd;
	float speed_rpm;
} dm_measure_t;

extern void clear_errors_J4310motor(uint16_t id);
extern void start_J4310motor(uint16_t id);
extern void lock_J4310motor(uint16_t id);
extern void save_zero_J4310motor(uint16_t id);\

extern void ctrl_J4310motor_speed(uint16_t id, float _vel);
extern void ctrl_J4310motor_speed_pos(uint16_t id, float _pos, float _vel);
extern void ctrl_J4310motor_mit(uint16_t id, float _pos, float _vel, float _KP, float _KD, float _torq);

extern void CAN_cmd_ore_extraction(int16_t sidesway, int16_t sucker_roll, int16_t ore_flip_l, int16_t ore_flip_r);
extern void CAN_cmd_rescue(int16_t rescue_l, int16_t rescue_r, int16_t rev1, int16_t rev2);
extern void CAN_cmd_chassis_reset_ID(void);

extern void Publish_Info_To_CAN2(uint8_t Info[8]);

extern const dm_measure_t 	 *get_sucker_yaw_measure_point(void);
extern const dm_measure_t 	 *get_sucker_pitch_F_measure_point(void);
extern const dm_measure_t 	 *get_sucker_pitch_B_measure_point(void);

extern const motor_measure_t *get_sidesway_motor_measure_point(void);
extern const motor_measure_t *get_sucker_roll_motor_measure_point(void);
extern const motor_measure_t *get_telescope_ex_l_motor_measure_point(void);
extern const motor_measure_t *get_telescope_ex_r_motor_measure_point(void);
extern const motor_measure_t *get_rescue_l_motor_measure_point(void);
extern const motor_measure_t *get_rescue_r_motor_measure_point(void);

#endif
